﻿using LY.Manage.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LY.Manage.WebApp.Controllers
{
    public class CanShuController : BaseController
    {
        //
        // GET: /CanShu/

      
        //
        // GET: /CanShu/
        IBLL.Itbl_canshuService CanShuService { get; set; }
        public ActionResult Index()
        {
            return View();
        }

        #region 获取列表数据 GetDataInfoList  (手动设置返回的数据列)
        public ActionResult GetDataInfoList()
        {


            int pageIndex = Request["page"] != null ? int.Parse(Request["page"]) : 1;
            int pageSize = Request["rows"] != null ? int.Parse(Request["rows"]) : 20;
            int totalCount = 0;

            var dataList = CanShuService.LoadPageEntities<int>
                (pageIndex, pageSize,
                out totalCount,

                 u => true,
                u => u.id,
                true
                );
            var temp = from r in dataList.ToList()
                       select new
                       {
                           r.id,
                           r.canshu,
                           r.value,
                           r.note


                       };
            return Json(new
            {
                rows = temp,
                total = totalCount
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 显示编辑页面 ShowEditInfo
        public ActionResult ShowEditInfo()
        {
            int id = int.Parse(Request["id"]);

            var model = CanShuService.LoadEntities(u => u.id == id).FirstOrDefault();

            return View(model);
        }
        #endregion

        public ActionResult EditInfo(tbl_canshu entity)
        {


            WsResult ws = new WsResult();
            //1.根据主键ID查询数据  oldentity 说明是数据库原来存储的实体对象
            var oldentity = CanShuService.LoadEntities(u => u.id == entity.id).FirstOrDefault();
            if (entity == null)
            {

                ws.Msg = "编辑的ID不存在!";
                return Json(ws);
            }

            #region 2.设置要修改的数据
            //用户表应该修改什么字段

            oldentity.note = entity.note;
            oldentity.canshu = entity.canshu;
            oldentity.value = entity.value;
            #endregion


            //3、将更新完的数据,更新到数据中
            if (CanShuService.UpdateEntity(oldentity))
            {
                ws.IsSuccess = true;
                return Json(ws);
            }
            else
            {
                ws.Msg = "更新失败!";
                return Json(ws);
            }
        }

    }

}

